/*
day268
2020.5.22
*/
#include<vector>
#include<algorithm>
#include<stack>
#include<iostream> 
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
	stack<TreeNode *> stack;
	TreeNode *cur;
	
    TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
    	cur=root; 
		while(next()!=p){}
		return next();
    }
    
    TreeNode* next()
    {
    	while(cur)
    	{
    		stack.push(cur);
    		cur=cur->left;
		}
		
		if(stack.empty())
		{
			return nullptr;
		 } 
		 
		TreeNode *res=stack.top();
		stack.pop();
		cur=res->right;
		
		return res;
	}
};

int main()
{
	return 0;
 } 
